home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / MK_PRIV.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  6.7 KB  |  227 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/krb/RCS/mk_priv.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  *
  11.  * This routine constructs a Kerberos 'private msg', i.e.
  12.  * cryptographically sealed with a private session key.
  13.  *
  14.  * Note-- bcopy is used to avoid alignment problems on IBM RT.
  15.  *
  16.  * Note-- It's too bad that it did a long int compare on the RT before.
  17.  *
  18.  * Returns either < 0 ===> error, or resulting size of message
  19.  *
  20.  * Steve Miller    Project Athena  MIT/DEC
  21.  */
  22.  
  23. #ifndef lint
  24. static char *rcsid_mk_priv_c=
  25. "$Header: mk_priv.c,v 4.13 89/03/22 14:48:59 jtkohl Exp $";
  26. #endif /* lint */
  27.  
  28. #include <mit_copy.h>
  29.  
  30. /* system include files */
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <sys\types.h>
  34. #include <netinet\in.h>
  35. #include <sys/socket.h>
  36.  
  37. /* application include files */
  38. #include <des.h>
  39. #include <krb.h>
  40. #include <prot.h>
  41. #include "lsbaddcm.h"
  42.  
  43. extern char *errmsg();
  44. extern int krb_debug;
  45.  
  46. /* static storage */
  47.  
  48.  
  49. static u_long c_length;
  50. static struct timeval msg_time;
  51. static u_char msg_time_5ms;
  52. static long msg_time_sec;
  53.  
  54. /*
  55.  * krb_mk_priv() constructs an AUTH_MSG_PRIVATE message.  It takes
  56.  * some user data "in" of "length" bytes and creates a packet in "out"
  57.  * consisting of the user data, a timestamp, and the sender's network
  58.  * address.
  59. #ifndef NOENCRYTION
  60.  * The packet is encrypted by pcbc_encrypt(), using the given
  61.  * "key" and "schedule".
  62. #endif
  63.  * The length of the resulting packet "out" is
  64.  * returned.
  65.  *
  66.  * It is similar to krb_mk_safe() except for the additional key
  67.  * schedule argument "schedule" and the fact that the data is encrypted
  68.  * rather than appended with a checksum.  Also, the protocol version
  69.  * number is "private_msg_ver", defined in krb_rd_priv.c, rather than
  70.  * KRB_PROT_VERSION, defined in "krb.h".
  71.  *
  72.  * The "out" packet consists of:
  73.  *
  74.  * Size            Variable        Field
  75.  * ----            --------        -----
  76.  *
  77.  * 1 byte        private_msg_ver        protocol version number
  78.  * 1 byte        AUTH_MSG_PRIVATE |    message type plus local
  79.  *            HOST_BYTE_ORDER        byte order in low bit
  80.  *
  81. #ifdef NOENCRYPTION
  82.  * 4 bytes        c_length        length of data
  83. #else
  84.  * 4 bytes        c_length        length of encrypted data
  85.  *
  86.  * ===================== begin encrypt ================================
  87. #endif
  88.  * 
  89.  * 4 bytes        length            length of user data
  90.  * length        in            user data
  91.  * 1 byte        msg_time_5ms        timestamp milliseconds
  92.  * 4 bytes        sender->sin.addr.s_addr    sender's IP address
  93.  *
  94.  * 4 bytes        msg_time_sec or        timestamp seconds with
  95.  *            -msg_time_sec        direction in sign bit
  96.  *
  97.  * 0<=n<=7  bytes    pad to 8 byte multiple    zeroes
  98. #ifndef NOENCRYPTION
  99.  *            (done by pcbc_encrypt())
  100.  *
  101.  * ======================= end encrypt ================================
  102. #endif
  103.  */
  104.  
  105. long krb_mk_priv(in,out,length,schedule,key,sender,receiver)
  106.     u_char *in;                 /* application data */
  107.     u_char *out;                /* put msg here, leave room for
  108.                                  * header! breaks if in and out
  109.                                  * (header stuff) overlap */
  110.     u_long length;              /* of in data */
  111.     Key_schedule schedule;      /* precomputed key schedule */
  112.     C_Block key;                /* encryption key for seed and ivec */
  113.     struct sockaddr_in *sender; /* sender address */
  114.     struct sockaddr_in *receiver; /* receiver address */
  115. {
  116.     register u_char     *p,*q;
  117.     static       u_char *c_length_ptr;
  118.     extern int private_msg_ver; /* in krb_rd_priv.c */
  119.  
  120.     /*
  121.      * get the current time to use instead of a sequence #, since
  122.      * process lifetime may be shorter than the lifetime of a session
  123.      * key.
  124.      */
  125.     if (gettimeofday(&msg_time,(struct timezone *)0)) {
  126.         return -1;
  127.     }
  128.     msg_time_sec = (long) msg_time.tv_sec;
  129.     msg_time_5ms = msg_time.tv_usec/5000; /* 5ms quanta */
  130.  
  131.     p = out;
  132.  
  133.     *p++ = private_msg_ver;
  134.     *p++ = AUTH_MSG_PRIVATE | HOST_BYTE_ORDER;
  135.  
  136.     /* calculate cipher length */
  137.     c_length_ptr = p;
  138.     p += sizeof(c_length);
  139.  
  140. #ifndef NOENCRYPTION
  141.     /* start for encrypted stuff */
  142. #endif
  143.     q = p;
  144.  
  145.     /* stuff input length */
  146.     bcopy((char *)&length,(char *)p,sizeof(length));
  147.     p += sizeof(length);
  148.  
  149. #ifdef NOENCRYPTION
  150.     /* make all the stuff contiguous for checksum */
  151. #else
  152.     /* make all the stuff contiguous for checksum and encryption */
  153. #endif
  154.     bcopy((char *)in,(char *)p,(int) length);
  155.     p += length;
  156.  
  157.     /* stuff time 5ms */
  158.     bcopy((char *)&msg_time_5ms,(char *)p,sizeof(msg_time_5ms));
  159.     p += sizeof(msg_time_5ms);
  160.  
  161.     /* stuff source address */
  162.     bcopy((char *)&sender->sin_addr.s_addr,(char *)p,
  163.           sizeof(sender->sin_addr.s_addr));
  164.     p += sizeof(sender->sin_addr.s_addr);
  165.  
  166.     /*
  167.      * direction bit is the sign bit of the timestamp.  Ok
  168.      * until 2038??
  169.      */
  170.     /* For compatibility with broken old code, compares are done in VAX 
  171.        byte order (LSBFIRST) */ 
  172.     if (lsb_net_ulong_less(sender->sin_addr.s_addr, /* src < recv */ 
  173.               receiver->sin_addr.s_addr)==-1) 
  174.         msg_time_sec =  -msg_time_sec; 
  175.     else if (lsb_net_ulong_less(sender->sin_addr.s_addr, 
  176.                 receiver->sin_addr.s_addr)==0) 
  177.         if (lsb_net_ushort_less(sender->sin_port,receiver->sin_port) == -1) 
  178.             msg_time_sec = -msg_time_sec; 
  179.     /* stuff time sec */
  180.     bcopy((char *)&msg_time_sec,(char *)p,sizeof(msg_time_sec));
  181.     p += sizeof(msg_time_sec);
  182.  
  183.     /*
  184.      * All that for one tiny bit!  Heaven help those that talk to
  185.      * themselves.
  186.      */
  187.  
  188. #ifdef notdef
  189.     /*
  190.      * calculate the checksum of the length, address, sequence, and
  191.      * inp data
  192.      */
  193.     cksum =  quad_cksum(q,NULL,p-q,0,key);
  194.     if (krb_debug)
  195.         printf("\ncksum = %u",cksum);
  196.     /* stuff checksum */
  197.     bcopy((char *) &cksum,(char *) p,sizeof(cksum));
  198.     p += sizeof(cksum);
  199. #endif
  200.  
  201. #ifdef NOENCRYPTION
  202.     /*
  203.      * All the data have been assembled, compute length
  204.      */
  205. #else
  206.     /*
  207.      * All the data have been assembled, compute length and encrypt
  208.      * starting with the length, data, and timestamps use the key as
  209.      * an ivec.
  210.      */
  211. #endif
  212.  
  213.     c_length = p - q;
  214.     c_length = ((c_length + sizeof(C_Block) -1)/sizeof(C_Block)) *
  215.         sizeof(C_Block);
  216.     /* stuff the length */
  217.     bcopy((char *) &c_length,(char *)c_length_ptr,sizeof(c_length));
  218.  
  219. #ifndef NOENCRYPTION
  220.     /* pcbc encrypt, pad as needed, use key as ivec */
  221.     pcbc_encrypt((C_Block *) q,(C_Block *) q, (long) (p-q), schedule,
  222.                  key, ENCRYPT);
  223. #endif /* NOENCRYPTION */
  224.  
  225.     return (q - out + c_length);        /* resulting size */
  226. }
  227.